<html>
<head>
<link rel="shortcut icon" href="./favicon.ico">
<link rel="stylesheet" type="text/css" href="./style.css">
<link rel="canonical" href="./Priority_Encoder.html">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Takes in a bitmask of multiple requests and returns the zero-based index of the set request bit with the highest priority. The least-significant bit has highest priority. If no request bits are set, the output is zero, but signalled as invalid.  This Priority Encoder is very closely related to the [Number of Trailing Zeros](./Number_of_Trailing_Zeros.html) module.">
<title>Priority Encoder</title>
</head>
<body>

<p class="inline bordered"><b><a href="./Priority_Encoder.v">Source</a></b></p>
<p class="inline bordered"><b><a href="./legal.html">License</a></b></p>
<p class="inline bordered"><b><a href="./index.html">Index</a></b></p>

<h1>A Priority Encoder</h1>
<p>Takes in a bitmask of multiple requests and returns the zero-based index of
 the set request bit with the highest priority. The least-significant bit
 has highest priority. If no request bits are set, the output is zero, but
 signalled as invalid.  This Priority Encoder is very closely related to the
 <a href="./Number_of_Trailing_Zeros.html">Number of Trailing Zeros</a> module. </p>
<p>For example:</p>
<ul>
<li>11111 --&gt; 00000 (0)</li>
<li>00010 --&gt; 00001 (1)</li>
<li>01100 --&gt; 00010 (2)</li>
<li>11000 --&gt; 00011 (3)</li>
<li>10000 --&gt; 00011 (4)</li>
<li>00000 --&gt; 00000 (0, invalid)</li>
</ul>
<p>The Priority Encoder translates bitmasks to integers, and so can be
 generally used to convert separate physical events into a number for later
 processing or to index into a table, while filtering out multiple
 simultaneous events into only one.</p>

<pre>
`default_nettype none

module <a href="./Priority_Encoder.html">Priority_Encoder</a>
#(
    parameter WORD_WIDTH = 0
)
(
    input   wire    [WORD_WIDTH-1:0]    word_in,
    output  wire    [WORD_WIDTH-1:0]    word_out,
    output  reg                         word_out_valid
);

    localparam WORD_ZERO = {WORD_WIDTH{1'b0}};

    initial begin
        word_out_valid  = 1'b0;
    end
</pre>

<p>First, isolate the least-significant 1 bit</p>

<pre>
    wire [WORD_WIDTH-1:0] lsb_1;

    <a href="./Bitmask_Isolate_Rightmost_1_Bit.html">Bitmask_Isolate_Rightmost_1_Bit</a>
    #(
        .WORD_WIDTH (WORD_WIDTH)
    )
    find_lsb_1
    (
        .word_in    (word_in),
        .word_out   (lsb_1)
    );
</pre>

<p>A single bit is a power of two, so take its logarithm, which returns its
 zero-based index, which is also the encoded priority.</p>

<pre>
    wire logarithm_undefined;

    <a href="./Logarithm_of_Powers_of_Two.html">Logarithm_of_Powers_of_Two</a>
    #(
        .WORD_WIDTH             (WORD_WIDTH)
    )
    calc_bit_index
    (
        .one_hot_in             (lsb_1),
        .logarithm_out          (word_out),
        .logarithm_undefined    (logarithm_undefined)
    );
</pre>

<p>However, there is a corner case: if the input word is all zero then the
 logarithm output is undefined, and so the output number is zero as if the
 zeroth bit was set, but invalid.</p>

<pre>
    always @(*) begin
        word_out_valid = (logarithm_undefined == 1'b0);
    end

endmodule
</pre>

<hr>
<p><a href="./index.html">Back to FPGA Design Elements</a>
<center><a href="https://fpgacpu.ca/">fpgacpu.ca</a></center>
</body>
</html>

